/**
 * 
 */
package com.shy.portal.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.shy.base.aop.LogPoint;
import com.shy.base.service.impl.BaseServiceImpl;
import com.shy.base.shiro.ShiroUser;
import com.shy.base.util.IpUtil;
import com.shy.base.vo.AppConstants;
import com.shy.portal.dao.UserLogDao;
import com.shy.portal.entity.UserLog;
import com.shy.portal.service.UserLogService;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;

/**
 * <p>Title:UserLogServiceImpl </p>
 * <p>Description: 系统操作日志 service层实现</p>
 * <p>Company:ipebg </p> 
 * @author H2013788
 * @date 2019年07月12日
 */
@Transactional
@Service("logService")
public class UserLogServiceImpl extends BaseServiceImpl<UserLog> implements UserLogService, LogPoint {
	
	private static final String LOG_CONTENT = "[类名]:%s,[方法]:%s,[参数]:%s,[IP]:%s";
	
	/**
     * dao
     */
    @Resource
    private UserLogDao logDao;

    @Resource
    private void setBaseDao(UserLogDao logDao) {
        this.baseDao = logDao;
    }
    
    @Override
	public void save(JoinPoint joinPoint, String methodName, String module, String description) {
		ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
	    if (attributes!=null) {
	        HttpServletRequest request = attributes.getRequest();
	        String username = "";
	        ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			if(user != null) {
				username = user.getLoginName();
			}else {
	            username = request.getParameter(AppConstants.LOGIN_USER_PARAM);
	        }
	        String ip = IpUtil.getIpAddr(request);
	        UserLog log = new UserLog();
	        UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
            if(null != userAgent) {
                log.setUpdateUser(userAgent.getBrowser().toString() + userAgent.getBrowser().getVersion(request.getHeader("User-Agent")));
            }
	        log.setCreateTime(new Date());
	        log.setUserNo(username);
	        log.setModule(module);
	        log.setOpration(description);
	        log.setIp(ip);
	        log.setUrl(request.getRequestURI().toString());
	        log.setDescription(operateContent(joinPoint, methodName, ip, request));
	        super.save(log);
        }	
	}
	
	
	public String operateContent(JoinPoint joinPoint, String methodName, String ip, HttpServletRequest request) {
        String className = joinPoint.getTarget().getClass().getName();
        Object[] params = joinPoint.getArgs();
        StringBuffer bf = new StringBuffer();
        if (params != null && params.length > 0) {
            Enumeration<String> paraNames = request.getParameterNames();
            while (paraNames.hasMoreElements()) {
                String key = paraNames.nextElement();
                if(!CollectionUtil.contains(Arrays.asList(AppConstants.EXCLUDE_PARAMS_LOG), key)) {
	                bf.append(key).append("=");
	                bf.append(request.getParameter(key)).append("&");
                }
            }
            if (StringUtils.isBlank(bf.toString())) {
                bf.append(request.getQueryString());
            }
        }
        return String.format(LOG_CONTENT, className, methodName, bf.toString(), ip);
    }

}
